package com.sh.dc.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sh.dc.common.BaseResult;
import com.sh.dc.common.enums.BizCodeEnum;
import com.sh.dc.common.pojo.bo.TaskAlgoSaveBO;
import com.sh.dc.common.pojo.bo.TaskAlgoStatusAsyncBO;
import com.sh.dc.common.pojo.query.TaskAlgoPageQuery;
import com.sh.dc.entity.Algo;
import com.sh.dc.entity.TaskAlgo;
import com.sh.dc.service.IAlgoService;
import com.sh.dc.service.ITaskAlgoService;
import io.micrometer.common.util.StringUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Optional;

@RestController
@RequestMapping("/taskAlgo")
public class TaskAlgoController {

    @Autowired
    private ITaskAlgoService taskAlgoService;

    @Autowired
    private IAlgoService algoService;

    @PostMapping("/save")
    public BaseResult save(@Valid @RequestBody TaskAlgoSaveBO taskAlgoSaveBO) {
        TaskAlgo taskAlgo = new TaskAlgo();
        BeanUtil.copyProperties(taskAlgoSaveBO, taskAlgo);
        taskAlgo.setUpdateTime(new Date());
        if (taskAlgo.getAlgoId() != null && StringUtils.isBlank(taskAlgo.getAlgoName())) {
            Optional<Algo> opt = algoService.getOptById(taskAlgo.getAlgoId());
            if (!opt.isPresent()) {
                return BaseResult.fail(BizCodeEnum.ALGO_NOT_EXISTED);
            }
            taskAlgo.setAlgoName(opt.get().getName());
        }
        taskAlgoService.saveOrUpdate(taskAlgo);
        return BaseResult.success(taskAlgo.getId());
    }

    @PostMapping("/taskStatusAsync")
    public BaseResult taskStatusAsync(@Valid @RequestBody TaskAlgoStatusAsyncBO taskAlgoStatusAsyncBO) {
        Optional<TaskAlgo> taskAlgoOptional = taskAlgoService.getOneOpt(new QueryWrapper<TaskAlgo>().eq("tid", taskAlgoStatusAsyncBO.getTid()));
        if(!taskAlgoOptional.isPresent()) {
            return BaseResult.fail(BizCodeEnum.ALGO_NOT_EXISTED);
        }
        TaskAlgo taskAlgo = taskAlgoOptional.get();
        taskAlgo.setStatus(taskAlgoStatusAsyncBO.getStatus());
        if (taskAlgoStatusAsyncBO.getStartTime() != null) {
            taskAlgo.setStartTime(taskAlgoStatusAsyncBO.getStartTime());
        }
        if (taskAlgoStatusAsyncBO.getEndTime() != null) {
            taskAlgo.setEndTime(taskAlgoStatusAsyncBO.getEndTime());
        }
        if (StringUtils.isNotBlank(taskAlgoStatusAsyncBO.getResult())) {
            taskAlgo.setResult(taskAlgoStatusAsyncBO.getResult());
        }
        taskAlgoService.updateById(taskAlgo);
        return BaseResult.success();
    }

    @PostMapping("/page")
    public BaseResult page(@RequestBody TaskAlgoPageQuery query) {
        return BaseResult.success(taskAlgoService.getTaskAlgoPage(query));
    }

    @PostMapping("/delete/{id}")
    public BaseResult deleteById(@PathVariable("id") Integer id) {
        return BaseResult.success(taskAlgoService.removeById(id));
    }

}
